package com.shurdroides.proyectofinal.databasehelpers;

import com.shurdroides.proyectofinal.constants.Constants;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class OpenHelper extends SQLiteOpenHelper implements Constants.Database {
	Context mContext;
	
	public OpenHelper(Context context) {
		super(context, BBDD, null, 1);
		mContext = context;
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		createObjetoTable(db);
		createRecetasTable(db);
		createIngredientesTable(db);
		createResTable(db);
		createLogroTable(db);
		createUserTable(db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLA_INGREDIENTES.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + TABLA_LOGRO.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + TABLA_OBJETO.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + TABLA_RESULTADOS.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + TABLA_USUARIO.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + TABLA_RECETAS.TABLE_NAME);
		
	}
	
	private void createObjetoTable(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE " + TABLA_OBJETO.TABLE_NAME + "(" +
         TABLA_OBJETO.COL_ID + " INTEGER" + PK + "," +
         TABLA_OBJETO.COL_NAME + " TEXT UNIQUE," +
         TABLA_OBJETO.COL_PESO + " INTEGER NOT NULL, " + 
         TABLA_OBJETO.COL_CANTIDAD_INV + " INTEGER NOT NULL, " +
         TABLA_OBJETO.COL_CANTIDAD_CASA + " INTEGER NOT NULL, " +
         TABLA_OBJETO.COL_FOTO + " TEXT NOT NULL, "+
         TABLA_OBJETO.COL_ALIMENTO + " INTEGER NOT_NULL, "+
         TABLA_OBJETO.COL_DURACION + " INTEGER NOT NULL," +
         TABLA_OBJETO.COL_DURACION_ABS + " INTEGER NOT NULL" +
		");");
	}
	
	private void createRecetasTable(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE " + TABLA_RECETAS.TABLE_NAME + "(" +
         TABLA_RECETAS.COL_ID + " INTEGER" +  PK + "," +
         TABLA_RECETAS.COL_PUNTOS + " INTEGER NOT NULL," + 
         TABLA_RECETAS.COL_DESCUBIERTO + " INTEGER NOT NULL" +
         ");");
	}
	
	private void createIngredientesTable(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE " + TABLA_INGREDIENTES.TABLE_NAME + "(" +
		 TABLA_INGREDIENTES.COL_ID + " INTEGER" + PK +  "," +
		 TABLA_INGREDIENTES.COL_ID_OBJETO + REF + TABLA_OBJETO.COL_ID + ", " +
         TABLA_INGREDIENTES.COL_ID_RECETA + REF + TABLA_OBJETO.COL_ID + ", " +
         TABLA_INGREDIENTES.COL_CANTIDAD + " INTEGER NOT NULL" +
         ");");
	}
	

	private void createResTable(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE " + TABLA_RESULTADOS.TABLE_NAME + "(" +
		 TABLA_RESULTADOS.COL_ID + " INTEGER" + PK +  "," +
         TABLA_RESULTADOS.COL_ID_OBJETO + REF + TABLA_OBJETO.COL_ID + ", " +
         TABLA_RESULTADOS.COL_ID_RECETA + REF + TABLA_OBJETO.COL_ID + ", " +
         TABLA_RESULTADOS.COL_CANTIDAD + " INTEGER NOT NULL" + 
		");");
	}
	
	private void createLogroTable(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE " + TABLA_LOGRO.TABLE_NAME + "(" +
         TABLA_LOGRO.COL_ID + " INTEGER" + PK + ", " +
         TABLA_LOGRO.COL_PARENT_ID + " INTEGER" + REF + TABLA_LOGRO.COL_ID + ", " +
         TABLA_LOGRO.COL_NOMBRE + " TEXT NOT NULL, " +
         TABLA_LOGRO.COL_CONDICION + " INTEGER NOT NULL ," +
         TABLA_LOGRO.COL_ADQUIRIDO + " INTEGER NOT NULL ," +
         TABLA_LOGRO.COL_HABILIDAD + " TEXT NOT NULL" +
         ");");
	}
	
	private void createUserTable(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE " + TABLA_USUARIO.TABLE_NAME + "(" +
         TABLA_USUARIO.COL_ID + " INTEGER PRIMARY KEY, " +
         TABLA_USUARIO.COL_NOMBRE + " TEXT NOT NULL, " +
         TABLA_USUARIO.COL_MAIL + " TEXT NOT NULL ," +
         TABLA_USUARIO.COL_LAT  + " INTEGER NOT NULL ," +
         TABLA_USUARIO.COL_LONG  + " INTEGER NOT NULL ," +
         TABLA_USUARIO.COL_CLASE + " TEXT NOT NULL ," +
         TABLA_USUARIO.COL_PUNTOS  + " INTEGER NOT NULL ," +
         TABLA_USUARIO.COL_FUERZA_MAX  + " INTEGER NOT NULL ," +
         TABLA_USUARIO.COL_RADIO_VISION + " INTEGER NOT NULL ," +
         TABLA_USUARIO.COL_RADIO_ACCION  + " INTEGER NOT NULL ," +
         TABLA_USUARIO.COL_CAZA  + " INTEGER NOT NULL ," +
         TABLA_USUARIO.COL_ROBO  + " INTEGER NOT NULL" +
		");");
	}
	
}
